package cn.group.controller;


import cn.group.domain.Course;
import cn.group.domain.SC;
import cn.group.domain.Student;
import cn.group.service.CourseService;
import cn.group.service.SCService;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sc")
public class SCController {
    @Autowired
    private SCService scService;
    @Autowired
    private CourseService courseService;

    @GetMapping("/showSC")
    public String getSCShow(ModelMap map){
            List<Course> courseList=courseService.queryAllCourse();
            map.addAttribute("coursemap",courseList);
            //System.out.println(map);
            Map<String,Object> result=new HashMap<>();
            result.put("courseList",courseList);
            return "scInfoIndex";
    }

    //获取全部成绩信息
    @RequestMapping("/getSCList")
    @ResponseBody
    public Map<String,Object> getSCList(){
        List<SC> scList=scService.getAll();
        /*for (SC sc : scList) {
            System.out.println(sc);
        }*/
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("scList",scList);
        result.put("message","获取班级信息成功");
        return result;
    }

    //分页查询
    @RequestMapping("/getSCListByPage")
    @ResponseBody
    public Map<String,Object> getSCListByPage(Integer currentPage,Integer pageSize,String role,Integer id){
        PageInfo<SC> pageInfo = null;
        if(role.equals("m_teacher")) {
            pageInfo = scService.queryAll(currentPage, pageSize);
        }else if(role.equals("h_teacher")){
            pageInfo = scService.queryByHteacherId(currentPage, pageSize,id);
        }else if(role.equals("t_teacher")){
            pageInfo = scService.queryBytteacherId(currentPage, pageSize,id);
        }else if(role.equals("student")){
            pageInfo = scService.getAllStu(currentPage, pageSize,id);
        }
        //System.out.println(pageInfo.getList());
        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("scList",pageInfo.getList());
        System.out.println("查询到的成绩：");
        for (SC sc : pageInfo.getList()) {
            System.out.println(sc);
        }
        result.put("totalPage",pageInfo.getPages());
        result.put("message","获取用户数据成功");
        return result;
    }
    //拿到一条数据信息
    @RequestMapping("/gerOneSC")
    @ResponseBody
    public Map<String,Object> getOneSC(Integer id){
        Map<String,Object> result=new HashMap<>();
        SC scSet =scService.getOneById(id);
        if (scSet==null){
            result.put("code",-1);
            result.put("message","获取数据失败");
        }else {
            result.put("code",0);
            result.put("message","获取数据成功");
            result.put("scSet",scSet);
        }
        return result;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(@RequestBody SC sc){
        Map<String,Object> result=new HashMap<>();
        boolean changed;
        changed= scService.changeOne(sc);
        if(!changed){
            result.put("code", -1);
            result.put("message", "添加/编辑数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "添加/编辑数据成功");
        }
        return result;
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String,Object> add(String studentId,String courseId,String score){
        System.out.println("获得的数据"+studentId+","+courseId+","+score);
        Map<String,Object> result=new HashMap<>();
        boolean changed;
        /*changed = scService.addOne(sc,cid,score);
        if(!changed){
            result.put("code", -1);
            result.put("message", "添加/编辑数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "添加/编辑数据成功");
        }*/
        return result;
    }

}
